﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using PDOGenerator.Util;

namespace PDOGenerator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class PdoApp : Application
    {
        private GeneratorPM _pm;

        public GeneratorPM PM
        {
            get { return _pm; }
            set { _pm = value; }
        }

        public PdoApp()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);

            _pm = new GeneratorPM();
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            DisplayException(e.Exception, false);
            e.Handled = true;
        }

        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            DisplayException((Exception)e.ExceptionObject, e.IsTerminating);
        }

        public static void DisplayException(Exception ex, bool isTerminating)
        {
            MessageDialog dlg = new MessageDialog();

            dlg.Caption = "Error";
            dlg.Buttons = MessageDialogButtons.OK;
            dlg.DefaultButton = MessageDialogResults.OK;
            if (isTerminating)
            {
                dlg.Header = "An unrecoverable error occured";
                dlg.MessageIcon = MessageDialogIcons.Stop;
                dlg.FooterText = "Click OK to close PDO Generator.";
            }
            else
            {
                dlg.Header = "A problem occured";
                dlg.MessageIcon = MessageDialogIcons.Warning;
                dlg.FooterText = "Click OK to continue.";
            }
            dlg.FooterIcon = MessageDialogIcons.Shield;

            dlg.Message = ex.Message;

            dlg.DetailsText = ex.ToString();

            dlg.ShowDialog();
        }
    }
}
